﻿using MyAlbum.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System;
using System.Linq;
using PersonalAlbum.Exceptions;
namespace UnitTests
{
    
    
    /// <summary>
    ///This is a test class for EntranceServiceTest and is intended
    ///to contain all EntranceServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EntranceServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

      [TestInitialize()]
        public void MyTestInitialize()
        {
            _testConfig = new TestConfig();
            enrances = new List<MyAlbum.Model.Entrance>();
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            _testConfig.Dispose();
            foreach (var item in enrances)
            {
                Service.DeleteItem(item);
            }
        }
        //
        private TestConfig _testConfig;
        private List<MyAlbum.Model.Entrance> enrances;
        MyAlbum.Services.EntranceService Service
        {
            get
            {
                return ObjectFactory.GetInstance<IDataService<MyAlbum.Model.Entrance>>() as EntranceService;
            }
        }

       
        /// <summary>
        ///A test for GetItems
        ///</summary>
        [TestMethod()]
        public void GetItemsTest()
        {
            var target = this.Service;
            string value = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Entrance item =
                new MyAlbum.Model.Entrance()
                {
                    Details = value,
                    IP = value.Substring(0, 10),
                    Page = value,
                     SessionId= value.Substring(0, 10),
                    Timestamp = DateTime.Now
                };
            target.AddItem(item,new MyAlbum.Utility.GenericValidationSummary());
            enrances.Add(item);
            var saved = target.GetItems().SingleOrDefault(a => a.Id == item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Details, saved.Details);

            Assert.AreEqual(item.IP.Substring(0, 10), saved.IP.Trim());//we need the trim, the value is defined as char
            Assert.AreEqual(item.Page, saved.Page);
            Assert.AreEqual(item.SessionId.Substring(0, 10), saved.SessionId.Trim());//we need the trim, the value is defined as char
        }

        /// <summary>
        ///A test for GetItemById
        ///</summary>
        [TestMethod()]
        public void GetItemByIdTest()
        {
            var target = this.Service;
            string value = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Entrance item =
                new MyAlbum.Model.Entrance()
                {
                    Details = value,
                    IP = value.Substring(0, 10),
                    Page = value,
                    SessionId = value.Substring(0, 10),
                    Timestamp = DateTime.Now
                };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            enrances.Add(item);
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(item.Details, saved.Details);

            Assert.AreEqual(item.IP.Substring(0, 10), saved.IP.Trim());//we need the trim, the value is defined as char
            Assert.AreEqual(item.Page, saved.Page);
            Assert.AreEqual(item.SessionId.Substring(0, 10), saved.SessionId.Trim());//we need the trim, the value is defined as char
        }
         /// <summary>
        ///A test for DeleteItem
        ///</summary>
        [TestMethod()]
        public void DeleteItem_Bad_ID()
        {
            var target = this.Service;
            target.DeleteItem(new MyAlbum.Model.Entrance() { Id = -1 * new Random().Next() });

        }
        /// <summary>
        ///A test for DeleteItem
        ///</summary>
        [TestMethod()]
        public void DeleteItemTest()
        {
            var target = this.Service;
          
            string value = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Entrance item =
                new MyAlbum.Model.Entrance()
                {
                    Details = value,
                    IP = value.Substring(0, 10),
                    Page = value,
                     SessionId = value.Substring(0, 10),
                    Timestamp = DateTime.Now
                };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.enrances.Add(item);
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);

            target.DeleteItem(item);
            saved = target.GetItemById(item.Id);
            Assert.IsNull(saved);
        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        public void AddItemTest()
        {
            var target = this.Service;
            string value = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Entrance item =
                new MyAlbum.Model.Entrance()
                {
                    Details = value,
                    IP = value.Substring(0, 10),
                    Page = value,
                     SessionId = value.Substring(0, 10),
                    Timestamp = DateTime.Now
                };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.enrances.Add(item);
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(value, saved.Details);
            Assert.AreEqual(value, saved.Details);
            Assert.AreEqual(value.Substring(0, 10), saved.IP.Trim());
            Assert.AreEqual(value, saved.Page);
            Assert.AreEqual(value.Substring(0, 10), saved.SessionId.Trim());
        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        public void AddItem_Null_Details()
        {
            var target = this.Service;
            string value = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Entrance item =
                new MyAlbum.Model.Entrance()
                {
                    IP = value.Substring(0, 10),
                    Page = value,
                    SessionId = value.Substring(0, 10),
                    Timestamp = DateTime.Now
                };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.enrances.Add(item);
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.IsTrue(string.IsNullOrEmpty( saved.Details));
            Assert.AreEqual(value.Substring(0, 10), saved.IP.Trim());
            Assert.AreEqual(value, saved.Page);
            Assert.AreEqual(value.Substring(0, 10), saved.SessionId.Trim());
        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
    [ExpectedException(typeof(AssertionException))]
        public void AddItem_Null_IP()
        {
            var target = this.Service;
            string value = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Entrance item =
                new MyAlbum.Model.Entrance()
                {
                    Details = value.Substring(0, 10),
                    Page = value,
                    SessionId = value.Substring(0, 10),
                    Timestamp = DateTime.Now
                };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.enrances.Add(item);
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(value.Substring(0, 10), saved.Details);
            //Assert.AreEqual(value, saved.Details);
            Assert.AreEqual(string.Empty, saved.IP);
            Assert.AreEqual(value, saved.Page);
            Assert.AreEqual(value.Substring(0, 10), saved.SessionId.Trim());
        }

         /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(AssertionException))]
        public void AddItem_Null_Item()
        {
            var target = this.Service;

            target.AddItem(null, new MyAlbum.Utility.GenericValidationSummary());
        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(AssertionException))]
        public void AddItem_Null_Page()
        {
            var target = this.Service;
            string value = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Entrance item =
                new MyAlbum.Model.Entrance()
                {
                    IP = value.Substring(0, 10),
                    Details = value,
                    SessionId = value.Substring(0, 10),
                    Timestamp = DateTime.Now
                };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.enrances.Add(item);
           
        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(AssertionException))]
        public void AddItem_Null_Session()
        {
            var target = this.Service;
            string value = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Entrance item =
                new MyAlbum.Model.Entrance()
                {
                    IP = value.Substring(0, 10),
                    Page = value,
                    Details = value.Substring(0, 10),
                    Timestamp = DateTime.Now
                };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.enrances.Add(item);
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);
            Assert.AreEqual(value.Substring(0, 10), saved.Details);
            //Assert.AreEqual(value, saved.Details);
            Assert.AreEqual(value.Substring(0, 10), saved.IP.Trim());
            Assert.AreEqual(value, saved.Page);
            Assert.AreEqual(string.Empty, saved.SessionId.Trim());
        }

        /// <summary>
        ///A test for AddItem
        ///</summary>
        [TestMethod()]
        public void AddItem_Null_TimeStamp()
        {
            var target = this.Service;
            string value = Guid.NewGuid().ToString("N");
            MyAlbum.Model.Entrance item =
                new MyAlbum.Model.Entrance()
                {
                    IP = value.Substring(0, 10),
                    Page = value,
                    SessionId = value.Substring(0, 10),
                    Details = value,
                     Timestamp=DateTime.Now
                };
            target.AddItem(item, new MyAlbum.Utility.GenericValidationSummary());
            this.enrances.Add(item);
            var saved = target.GetItemById(item.Id);
            Assert.IsNotNull(saved);           
            Assert.AreEqual(value, saved.Details);
            Assert.AreEqual(value.Substring(0, 10), saved.IP.Trim());
            Assert.AreEqual(value, saved.Page);
            Assert.AreEqual(value.Substring(0, 10), saved.SessionId.Trim());
        }
        /// <summary>
        ///A test for EntranceService Constructor
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void EntranceServiceConstructorTest()
        {
            MyAlbum.Data.IRepository<MyAlbum.Model.Entrance> entranceRepository = null;
            MyAlbum.Services.EntranceService target = new MyAlbum.Services.EntranceService(entranceRepository);
        }
    }
}
